package com.button.boot.active.service.impl;

import java.time.LocalDateTime;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ScheduledMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import com.button.boot.active.model.User;
import com.button.boot.active.service.ActiveMqService;

import net.sf.json.JSONObject;

@Service
public class ActiveMqServiceImpl implements ActiveMqService {
    private static final Logger log = LoggerFactory.getLogger(ActiveMqServiceImpl.class);
    
    @Autowired
    private Queue queueString;

    @Autowired
    private Queue queueUser;

    @Autowired
    private Queue queueReturn;

    @Autowired
    private Topic topicString;

    @Autowired
    private Topic topicUser;

    @Autowired
    private Topic delayTopicString;

    @Autowired
    private Queue queueStringACK;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Override
    public void sendQueueMsg(String msg) throws JMSException {
        log.info("sendQueueMsg-msg开始发送信息:{} --> {}", msg, this.queueString.getQueueName());
        this.jmsMessagingTemplate.convertAndSend(this.queueString, msg);
    }
    @Override 
    public void sendQueueMsg(User user) throws JMSException {
        log.info("sendQueueMsg-user开始发送信息:{} --> {}", JSONObject.fromObject(user), this.queueUser.getQueueName());
        this.jmsMessagingTemplate.convertAndSend(this.queueUser, user);
    }
    @Override
    public void send2Return(String msg) throws JMSException {
        log.info("send2WayQueueMsg-msg开始发送信息:{} --> {}", msg, this.queueReturn.getQueueName());
        this.jmsMessagingTemplate.convertAndSend(this.queueReturn,msg);
    }

    @Override
    public void sendACKQueueMsg(String msg) throws JMSException {
        log.info("sendACKQueueMsg开始发送信息:{} --> {}", msg, this.queueStringACK.getQueueName());
        this.jmsMessagingTemplate.convertAndSend(this.queueStringACK,msg);
    }

    

    @Override
    public void sendTopicMsg(String msg) throws JMSException {
        log.info("sendTopicMsg-msg发送信息:{} --> {}", msg, this.topicString.getTopicName());
        this.jmsMessagingTemplate.convertAndSend(this.topicString,msg);
    }

    @Override
    public void sendTopicMsg(User user) throws JMSException {
        log.info("sendTopicMsg-user发送信息:{} --> {}", JSONObject.fromObject(user), this.topicUser.getTopicName());
        this.jmsMessagingTemplate.convertAndSend(this.topicUser,user);
    }

    @Override
    public void sendDelayTopicMsg(String msg) throws JMSException {
        log.info("sendDelayTopicMsg发送延时信息:{} --> {},发送时间:{}", msg, this.delayTopicString.getTopicName(), LocalDateTime.now());
        this.jmsMessagingTemplate.getJmsTemplate().send(this.delayTopicString, session -> {
            TextMessage tx = session.createTextMessage(msg);
            tx.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 10 * 1000);
            return tx;
        });
    }

    @Override
    public void sendDelayTopicMsg(String msg,long time) throws JMSException {
        log.info("sendDelayTopicMsg发送延时信息:{} --> {},发送时间:{}", msg, this.delayTopicString.getTopicName(), LocalDateTime.now());
        this.jmsMessagingTemplate.getJmsTemplate().send(this.delayTopicString, session -> {
            TextMessage tx = session.createTextMessage(msg);
            tx.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
            return tx;
        });
    }
}
